package com.software.service.impl;

import java.util.List;

import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.software.dao.AgentDao;
import com.software.model.DrmAgentImg;
import com.software.model.PageListModel;
import com.software.model.PageModel;
import com.software.service.ImgMngService;
import com.software.utils.Constants;
import com.software.utils.IdCreater;

@Transactional
@Service("imgMngService")
public class ImgMngServiceImpl implements ImgMngService {

	@Autowired
	private AgentDao agentDao;

	private int beginRow;

	private int endRow;

	@Override
	public void saveAgentImg(DrmAgentImg data) {
		// img 插入数据
		agentDao.insertDrmAgentImg(data);

		data.setId(IdCreater.getId());
		// log 插入
		agentDao.insertDrmAgentImgLog(data);
	}

	@Override
	public void updateAgentDel(DrmAgentImg data) {
		// img 数据理论删除
		agentDao.updateImgDel(data);
		PageModel vo = new PageModel();
		vo.setIds(data.getId().toString());
		DrmAgentImg limgdata = agentDao.queryImgOne(vo);

		limgdata.setId(IdCreater.getId());
		// log 插入
		agentDao.insertDrmAgentImgLog(limgdata);
	}

	@Override
	public PageListModel searchAgentImgList(PageModel vo) {

		Long cnt = agentDao.queryImgCnt(vo);

		PageListModel val = new PageListModel();

		if (0 < cnt) {
			vo.setTotalNum(Integer.valueOf(cnt.toString()));

			beginRow = (vo.getPageNo() - 1) * vo.getPageSize();
			val.setPage(vo);

			endRow = vo.getPageNo() * (vo.getPageSize());

			System.out.println("beginRow = " + beginRow);

			System.out.println("endRow = " + endRow);
			vo.setStartLine(beginRow);
			vo.setTotalLine(vo.getPageSize());
			List<DrmAgentImg> list = agentDao.queryImgList(vo);

			for (DrmAgentImg img : list) {
				img.setImgKey(StringUtils.isNotBlank(img.getImgKey()) ? Constants.IMG_URL
						+ img.getImgKey() + "?w=150&h=130"
						: "");
				val.getShowData().add(img);
			}

		}

		return val;

	}

	@Override
	public List<DrmAgentImg> search5ImgList(PageModel vo) {
		List<DrmAgentImg> list = agentDao.query5ImgList(vo);
		for (DrmAgentImg img : list) {
			img.setImgKey(StringUtils.isNotBlank(img.getImgKey()) ? Constants.IMG_URL
					+ img.getImgKey() + "?w=150&h=130"
					: "");
		}
		return list;

	}

	@Override
	public DrmAgentImg searchImgOne(PageModel vo) {
		return agentDao.queryImgOne(vo);
	}

	@Override
	public void updateAgentImg(DrmAgentImg imgData) {
		// 更新图片信息
		agentDao.updateImgData(imgData);
		PageModel vo = new PageModel();
		vo.setIds(imgData.getId().toString());
		// 查询数据
		DrmAgentImg limgdata = agentDao.queryImgOne(vo);
		
		limgdata.setId(IdCreater.getId());
		// log 插入
		agentDao.insertDrmAgentImgLog(limgdata);

	}

}